Ti.include('functions.js');
var win = Ti.UI.currentWindow;
var data = [];

var tableview = Ti.UI.createTableView({
	visible: true,
	editable: false,
	top: 0,
	height: WindowWidth
});

win.addEventListener('open',function(e) {
	var udpModule = require('nl.icept.network.udp');
	var udpSocket = udpModule.createUDPSocket();

	udpSocketServer = udpModule.createUDPSocket({
		port: 12355
	});

		(function waitForClientPacket() {
			if (udpSocketServer != null) {
				if (udpSocketServer.isReadable()) {
					udpSocketServer.read(function(packet) {
        				var row = Ti.UI.createTableViewRow({
        					title:packet.data,
        					hasChild: true
        				});
        				data.push(packet);
        				tableview.appendRow(row);
					});
				}
				setTimeout(waitForClientPacket, 1000);
			}
		})();


	//send some packets!
	udpSocket.enableBroadcast();
	udpSocket.write("255.255.255.255", 12345, "beerwatch/find");
});

tableview.addEventListener('click',function(e) {
	var win1 = Titanium.UI.createWindow({  
    	title:'Beerwatch',
    	backgroundColor:'#fff',
    	beerwatch: e.rowData.title,
    	url: 'openbw.js'
	});
	Titanium.UI.currentTab.open(win1);
});

win.add(tableview);
